#!/bin/bash
# 清理日志文件

echo "清理PhotoEnhanceAI日志文件..."
echo "=========================================="

# 日志目录
LOG_DIRS=(
    "1-gateway/shared/logs"
    "4-management/01-gpu-autoscaling/logs"
)

# 清理天数 (保留最近7天的日志)
DAYS_TO_KEEP=7

for log_dir in "${LOG_DIRS[@]}"; do
    if [ -d "$log_dir" ]; then
        echo "清理目录: $log_dir"
        
        # 查找并删除7天前的日志文件
        find "$log_dir" -name "*.log" -type f -mtime +$DAYS_TO_KEEP -delete
        
        # 查找并压缩7天前的日志文件
        find "$log_dir" -name "*.log" -type f -mtime +1 -exec gzip {} \;
        
        echo "✅ $log_dir 清理完成"
    else
        echo "⚠️  目录不存在: $log_dir"
    fi
done

# 清理根目录的日志文件
echo ""
echo "清理根目录日志文件..."

ROOT_LOG_FILES=(
    "*.log"
    "access.log"
    "error.log"
    "debug.log"
)

for pattern in "${ROOT_LOG_FILES[@]}"; do
    find . -maxdepth 1 -name "$pattern" -type f -mtime +$DAYS_TO_KEEP -delete
done

echo "✅ 根目录日志清理完成"

# 显示磁盘使用情况
echo ""
echo "磁盘使用情况:"
echo "----------------------------------------"
df -h .

# 显示日志文件大小
echo ""
echo "当前日志文件大小:"
echo "----------------------------------------"
find . -name "*.log*" -type f -exec ls -lh {} \; | head -20

echo ""
echo "=========================================="
echo "日志清理完成"
